﻿#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Project逻辑代码【生成CopyToRuntime代码】
/// </summary>
public partial class ESF_Logic
{
    #region BuildAnimatorScript 生成AnimatorScript代码
    /// <summary>
    /// 生成AnimatorScript代码
    /// </summary>
    /// <returns>true:成功,false:失败</returns>
    [ESF_OneKeyBuild(ESF_enOneKeyBuildStep.BuildAnimatorScript)]
    [ESF_ProjectQuickButton("{0} {1}", ESF_enGUIDisplayWord.Build, ESF_enGUIDisplayWord.AnimatorScript)]
    public static bool BuildAnimatorScript()
    {
        bool result = true;
        if (projectSetting.xml.copyToXs != null)
        {
            var folder = projectSetting.xml.GetCopyToXFolder(ESF_enCopyToX.CopyToHF);
            folder = Path.Combine(folder,"Animator");
            ESF_Utility.ClearFolder(folder);

            ESF_BuildTextAsset bta = new ESF_BuildTextAsset("", folder, enESF_UnityFileExt.CS, "");

            Dictionary<string, ESF_SkillAnimatorControllerParameter> dicParameters = new Dictionary<string, ESF_SkillAnimatorControllerParameter>();
            Dictionary<string, ESF_SkillAnimatorControllerLayer> dicLayerNameIndexs = new Dictionary<string, ESF_SkillAnimatorControllerLayer>();
            Dictionary<string, int> dicLayerNames = new Dictionary<string, int>();           

            var ctls = ESF_Utility.CollectFile<ESF_SkillAnimatorCollectAsset>(enESF_AssetFilterClassify.AnimatorController);
            
            int count = ctls?.Count ?? 0;

            #region 解析Animator            
            foreach (var key in ctls)
            {
                foreach (var pt in key.aniCtrl.parameters)
                {
                    if (!dicParameters.ContainsKey(pt.Key))
                    {
                        dicParameters.Add(pt.Key, pt.Value);
                    }
                }
                foreach (var ly in key.aniCtrl.layerNameIndexs)
                {
                    if (!dicLayerNameIndexs.ContainsKey(ly.Key))
                    {
                        dicLayerNameIndexs.Add(ly.Key, ly.Value);
                    }
                    if (!dicLayerNames.ContainsKey(ly.Value.enLayerName))
                    {
                        dicLayerNames.Add(ly.Value.enLayerName, Animator.StringToHash(ly.Value.enLayerName));
                    }
                }
            }
            #endregion

            bool isCancel = false;
            if (!isCancel)
            {
                string absClsName = $"AbsESF_Animator";

                string attrLayerClsName = $"ESF_AnimatorControllLayerAttribute";
                string attrParamClsName = $"ESF_AnimatorControllParameterAttribute";

                string enClsName = $"ESF_AnimatorEnum";

                string layerFileName = $"{absClsName}_Layer";
                string parameterFileName = $"{absClsName}_Parameter";

                string enLayer = $"enESF_AnimatorControllerLayer";
                string enLayerNameIndex = $"enESF_AnimatorControllerLayerNameIndex";
                string enParameterName = $"enESF_AnimatorControllerParameter";

                string code = string.Empty;

                #region AbsESF_Animator 分部类
                code = File.ReadAllText(ESF_enResxTempleteFile.Compiler_Animator_Script_txt);
                code = code.CompilerCSharp(new
                {
                    absClsName,                    
                });
                bta.SetFileName(absClsName);
                bta.SetText(code);
                bta.CreateAsset();
                ESF_P.ESF_Log($"生成脚本=>{bta.filePath}");
                #endregion

                #region AnimatorControllLayerAttribute 属性
                code = File.ReadAllText(ESF_enResxTempleteFile.Compiler_AnimatorControllLayerAttribute_Script_txt);
                code = code.CompilerCSharp(new
                {
                    attrLayerClsName,
                    dicParameters,
                    dicLayerNameIndexs,
                    dicLayerNames,
                    enLayer,
                    enLayerNameIndex,
                    enParameterName,
                });
                bta.SetFileName(attrLayerClsName);
                bta.SetText(code);
                bta.CreateAsset();
                ESF_P.ESF_Log($"生成脚本=>{bta.filePath}");
                #endregion

                #region AnimatorControllParameterAttribute 属性
                code = File.ReadAllText(ESF_enResxTempleteFile.Compiler_AnimatorControllParameterAttribute_Script_txt);
                code = code.CompilerCSharp(new
                {
                    attrParamClsName,
                    dicParameters,
                    dicLayerNameIndexs,
                    dicLayerNames,
                    enLayer,
                    enLayerNameIndex,
                    enParameterName,
                });
                bta.SetFileName(attrParamClsName);
                bta.SetText(code);
                bta.CreateAsset();
                ESF_P.ESF_Log($"生成脚本=>{bta.filePath}");
                #endregion

                #region Animator Enum 枚举
                code = File.ReadAllText(ESF_enResxTempleteFile.Compiler_AnimatorEnum_Script_txt);
                code = code.CompilerCSharp(new
                {
                    enClsName,
                    dicParameters,
                    dicLayerNameIndexs,
                    dicLayerNames,
                    enLayer,
                    enLayerNameIndex,
                    enParameterName,
                });
                bta.SetFileName(enClsName);
                bta.SetText(code);
                bta.CreateAsset();
                ESF_P.ESF_Log($"生成脚本=>{bta.filePath}");
                #endregion

                #region AbsESF_Animator Layer 分部类
                code = File.ReadAllText(ESF_enResxTempleteFile.Compiler_Animator_Layer_Script_txt);
                code = code.CompilerCSharp(new
                {
                    absClsName,
                    dicParameters,
                    dicLayerNameIndexs,
                    dicLayerNames,
                    enLayer,
                    enLayerNameIndex,
                    enParameterName,
                });
                bta.SetFileName(layerFileName);
                bta.SetText(code);
                bta.CreateAsset();
                ESF_P.ESF_Log($"生成脚本=>{bta.filePath}");
                #endregion

                #region AbsESF_Animator Parameter 分部类
                code = File.ReadAllText(ESF_enResxTempleteFile.Compiler_Animator_Parameter_Script_txt);
                code = code.CompilerCSharp(new
                {
                    absClsName,
                    dicParameters,
                    dicLayerNameIndexs,
                    enLayer,
                    enLayerNameIndex,
                    enParameterName,
                });
                bta.SetFileName(parameterFileName);
                bta.SetText(code);
                bta.CreateAsset();
                ESF_P.ESF_Log($"生成脚本=>{bta.filePath}");
                #endregion
            }

            EditorUtility.ClearProgressBar();

            ESF_Utility.ExecuteMenuAssetsRefresh();
            EditorUtility.ClearProgressBar();
        }        
        ESF_P.ESF_Log($"BuildAnimatorScript Success.");
        return result;
    }
    #endregion
}
#endif
